<?php

/**
 * menu actions.
 *
 * @package    emc
 * @subpackage menu
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class menuActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $this->forward('default', 'module');
  }
public function executeList(sfWebRequest $request)
  {
    $this->menus = Doctrine_Core::getTable('Menu')->findByIsActive(true);
    $this->notify =  $request->getParameter('notify');
  	return sfView::SUCCESS;
  }
   public function executeEdit(sfWebRequest $request)
  {
    $data = $request->getParameter('menu', null);
    if ($data) {
        $menu =Doctrine_Core::getTable('Menu')->findOneByName($data['name']);
        $is_reactivate = false;
        if (!$data['menu_id'] && $menu)
            if ($menu->is_active == false) {
              $is_reactivate = true;
              $menu->is_active = true;
              $menu->save();
            } else {
              $this->notify = "menu_exists";
              $this->menu_form = new menuForm();
              $this->menu_form->bind($data);
              return sfView::SUCCESS;
            }
        if ($data['menu_id'])
          $menu = Doctrine_Core::getTable('Menu')->find($data['menu_id']);
        elseif (!$is_reactivate)
          $menu = new menu();
          
        try {
          $menu->name = $data['name'];
          $menu->description = $data['description'];
          $menu->href = $data['href'];
          if (!$is_reactivate)
          $menu->is_active = isset($data['is_active']) ? true : false;
          $menu->save();
          $this->notify = "create_menu_success";
          $this->menu_form = new MenuForm($menu);
        } catch (Exception $e) {
          $this->notify = "create_menu_failed";
        }
      } else {
        $this->menu_id = $request->getParameter('menu_id', null);
        if ($this->menu_id) { // it's an edit action
          $menu = Doctrine_Core::getTable('Menu')->find($this->menu_id);
          $this->menu_form = new menuForm($menu);
        } else {
          $this->menu_form = new menuForm();
        }
      }
    return sfView::SUCCESS;
  }
  public function executeDelete(sfWebRequest $request)
  {
    $menu_id = $request->getParameter('menu_id', null);
    $menu = Doctrine_Core::getTable('menu')->find($menu_id);
    if ($menu) {
      $menu->is_active = false;
      $menu->save();     
	  
	   try {
        $category = $menu->Categories;
        foreach ($category as $category) {
                  $category->is_active = false;
                  $category->save();
              }
               
        $this->getRequest()->setParameter('notify', "menu_deactive_success");
      } catch (Exception $e) {
          $this->getRequest()->setParameter('notify', "menu_deactive_failed");
      }
    }
	
    $this->forward('menu', 'list');
  }
}

